package com.anxin.common;

import lombok.Data;
import java.io.Serializable;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.hutool.core.collection.CollUtil;

@Data
public class Result<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMsg("操作成功");
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error(String msg) {
        return error(500, msg);
    }

    public static <T> Result<T> error(Integer code, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<PageResult<T>> page(IPage<T> page) {
        PageResult<T> pageResult = new PageResult<>(
            page.getTotal(),
            page.getPages(),
            page.getCurrent(),
            page.getSize(),
            CollUtil.newArrayList(page.getRecords())
        );
        Result<PageResult<T>> r = new Result<>();
        r.setCode(200);
        r.setMsg("操作成功");
        r.setData(pageResult);
        return r;
    }
} 